package demo;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * Socket客户端
 * 
 * @author sundy.hu
 * 
 * @version 1.0
 * 
 * @date 2012-11-28
 * 
 */
public class Client implements Runnable {

	// Socket 服务端地址
	private String address;

	// Socket服务端端口
	private int port;

	// 要发送的文件夹
	private File file;

	private static final int BUFFER_SIZE = 1024;

	// 设置要发送的文件夹
	public void setFile(File file) {
		this.file = file;
	}

	public Client(String address, int port) {
		this.address = address;
		this.port = port;
	}

	@Override
	public void run() {
		try {
			Socket client = new Socket(address, port);
			send(client);
		} catch (IOException ex) {
			ex.printStackTrace();
		}

	}

	// 发送文件
	private void send(Socket client) {
		if (null != client) {
			try {
				// 将要发送的文件打包
				String currentFileName = System.currentTimeMillis() + ".zip";
				ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(
						new FileOutputStream(currentFileName)));
				for (File f : file.listFiles()) {
					ZipEntry entry = new ZipEntry(f.getName());
					entry.setComment(String.valueOf(System.currentTimeMillis()));
					InputStream input = new FileInputStream(f);
					zos.putNextEntry(entry);
					System.out.println("getData ");
					int len = 0;
					while ((len = input.read()) != -1) {
						zos.write((char) len);
						if(1024*10==len){
							zos.flush() ;
						}
					}
					input.close();
				}
				zos.flush();
				zos.close();
				BufferedInputStream input = new BufferedInputStream(
						new FileInputStream(currentFileName));
				DataInputStream dis = new DataInputStream(input);
				BufferedOutputStream output = new BufferedOutputStream(
						client.getOutputStream());
				DataOutputStream dos = new DataOutputStream(output);
				int len = 0;
				client.setReceiveBufferSize(12 * BUFFER_SIZE);
				StringBuffer sb = new StringBuffer();
				while ((len = dis.read()) != -1) {
					dos.write(len);
					if (len == client.getReceiveBufferSize()) {
						dos.flush();
					}
					// output.write((char) len);
					sb.append((char) len);
				}
				System.out.println("send ..." + sb.toString().length());
				input.close();
				output.flush();
				output.close();
				client.close();
				new File(currentFileName).delete();
				System.out.println("files send successful");
				/*
				 * InputStream input = new FileInputStream(fileName); int len =
				 * 0; OutputStream output = client.getOutputStream(); while
				 * ((len = input.read()) != -1) { output.write((char) len); }
				 * input.close(); output.flush(); output.close();
				 * System.out.println(""); client.close();
				 */
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}
}
